from django.http import JsonResponse
from django.shortcuts import render, redirect

from news.dao.db import DBUtils,transation


def news_list(request):
    #查询新闻列表
    sql='select * from news_detail'
    res=DBUtils.select_more(sql)
    return render(request,'newslist.html',{'res':res})
@transation
def del_new(request,id):
    sql = 'delete from news_detail where id=%s'
    row = DBUtils.delete(sql,[id])
    if row>0:
        #重定向
        return redirect(to='/news/newslist/')
    else:
        #删除失败
        return render(request,'newslist.html',{'err':'删除失败'})
def detail(request,id):
    sql = 'select * from news_detail where id=%s'
    detail = DBUtils.select_one(sql,[id])
    return render(request,'detail.html',{'res':detail})

@transation
def register1(request):
    if request.method=='GET':
    #进入注册页面
        return render(request,'register1.html')
    else:
        #获取表单的提交数据
        userName = request.POST.get('userName')
        password = request.POST.get('password')
        email = request.POST.get('email')
        userType = request.POST.get('userType')
        sql = 'insert into news_user(userName,password,email,userType) values(%s,%s,%s,%s)'
        row,id = DBUtils.insert(sql,[userName,password,email,userType])
        if row>0:
            #注册成功进入登录页面
            return redirect(to='/news/login1/')
        else:
            return render(request,'register1.html',{'err':'注册失败'})

def login1(request):
    if request.method=='GET':
        #进入登录页面
        return render(request,'login1.html')
    else:
        #获取表单的提交数据
        userName = request.POST.get('userName')
        password = request.POST.get('password')
    sql='select * from news_user where userName=%s'
    data = DBUtils.select_one(sql,[userName])
    if not data:
        return render(request,'register.html')
    sql = 'select * from news_user where userName=%s and password=%s'
    data = DBUtils.select_one(sql,[userName,password])
    if data:
        #登录成功，进入首页
        return redirect(to='/news/newslist/')
    return render(request,'login1.html',{'err':'用户名或密码错误'})

@transation
def register2(request):
    if request.method=='GET':
    #进入注册页面
        return render(request,'register2.html')
    else:
        user=request.POST.dict()
        #获取表单的提交数据
        userName = request.POST.get('userName')
        password = request.POST.get('password')
        email = request.POST.get('email')
        userType = request.POST.get('userType')
        sql = 'insert into news_user(userName,password,email,userType) values(%s,%s,%s,%s)'
        row,id = DBUtils.insert(sql,[userName,password,email,userType])
        if row>0:
            #注册成功进入登录页面
            user['password']=''
            return JsonResponse({'code':200,'msg':'注册成功','data':user})
        else:
            return JsonResponse({'code':400,'msg':'注册失败'})

@transation
def login2(request):
    if request.method=='GET':
        #进入登录页面
        return render(request,'login2.html')
    else:
        #获取表单的提交数据
        userName = request.POST.get('userName')
        password = request.POST.get('password')
        sql='select * from news_user where userName=%s'
        data = DBUtils.select_one(sql,[userName])
    if not data: #是否注册
        return JsonResponse({'code':400,'msg':'用户不存在'})
    sql = 'select * from news_user where userName=%s and password=%s'
    data = DBUtils.select_one(sql,[userName,password])
    if data:
        #登录成功，进入首页
        return JsonResponse({'code':200,'msg':'登录成功'})
    return JsonResponse({'code':400,'msg':'用户名或密码错误'})


